import { Controller, Get, Query, Post, Body, UsePipes, ValidationPipe } from '@nestjs/common';
import { BaziService } from './bazi.service';
import { DatetimeService } from '../common/services/datetime.service';
import { SolarTime, Gender } from '../utils/bazi-utils';
import { 
  BaziInfoDto, 
  BaziDecadeDto, 
  BaziMonthDto, 
  BaziDayDto, 
  BaziHourDto,
  BaziPatternDto,
  BaziReverseDto
} from './dto/bazi-request.dto';

@Controller('bazi')
export class BaziController {
  constructor(
    private readonly baziService: BaziService,
    private readonly datetimeService: DatetimeService
  ) {}

  /**
   * 获取完整八字信息
   * GET /bazi/info?datetime=1990-01-01 12:00&gender=1&enableZiShiSupport=true
   */
  @Get('info')
  @UsePipes(new ValidationPipe({ transform: true }))
  getBaziInfo(@Query() query: BaziInfoDto) {
    const solarTime = this.datetimeService.parseToSolarTime(query.datetime);
    return this.baziService.getFullBaziInfo(
      solarTime, 
      query.gender, 
      query.name, 
      query.longitude, 
      query.latitude, 
      query.enableZiShiSupport
    );
  }

  /**
   * 获取大运列表（包含流年列表）
   * POST /bazi/decade
   */
  @Post('decade')
  @UsePipes(new ValidationPipe({ transform: true }))
  getDecadeList(@Body() body: BaziDecadeDto) {
    const solarTime = this.datetimeService.parseToSolarTime(body.datetime);
    return this.baziService.getDecadeFortuneList(solarTime, body.gender, body.enableZiShiSupport);
  }

  /**
   * 获取流月列表
   * POST /bazi/month
   */
  @Post('month')
  @UsePipes(new ValidationPipe({ transform: true }))
  getMonthList(@Body() body: BaziMonthDto) {
    const solarTime = this.datetimeService.parseToSolarTime(body.datetime);
    return this.baziService.getMonthList(solarTime, body.gender, body.decadeIndex, body.fortuneIndex, body.enableZiShiSupport);
  }

  /**
   * 获取流日列表
   * POST /bazi/day
   */
  @Post('day')
  @UsePipes(new ValidationPipe({ transform: true }))
  getDayList(@Body() body: BaziDayDto) {
    const solarTime = this.datetimeService.parseToSolarTime(body.datetime);
    return this.baziService.getDayList(solarTime, body.gender, body.decadeIndex, body.fortuneIndex, body.monthIndex, body.enableZiShiSupport);
  }

  /**
   * 获取流时列表
   * POST /bazi/hour
   */
  @Post('hour')
  @UsePipes(new ValidationPipe({ transform: true }))
  getHourList(@Body() body: BaziHourDto) {
    const solarTime = this.datetimeService.parseToSolarTime(body.datetime);
    return this.baziService.getHourList(solarTime, body.gender, body.decadeIndex, body.fortuneIndex, body.monthIndex, body.dayIndex, body.enableZiShiSupport);
  }

  /**
   * 获取格局分析
   * GET /bazi/pattern?datetime=1990-01-01 12:00&gender=1
   */
  @Get('pattern')
  @UsePipes(new ValidationPipe({ transform: true }))
  getPatternAnalysis(@Query() query: BaziPatternDto) {
    const solarTime = this.datetimeService.parseToSolarTime(query.datetime);
    return this.baziService.getPatternAnalysis(solarTime, query.gender, query.enableZiShiSupport);
  }

  /**
   * 获取当前时间八字信息
   * GET /bazi/current
   */
  @Get('current')
  getCurrentTimeBazi() {
    const now = new Date();
    const currentDateTime = now.toLocaleString('zh-CN', {
      year: 'numeric',
      month: '2-digit',
      day: '2-digit',
      hour: '2-digit',
      minute: '2-digit',
      second: '2-digit'
    }).replace(/\//g, '-');
    
    const solarTime = this.datetimeService.parseToSolarTime(currentDateTime);
    return this.baziService.getCurrentTimeBazi(solarTime);
  }

  /**
   * 根据八字反推日期
   * POST /bazi/reverse
   */
  @Post('reverse')
  @UsePipes(new ValidationPipe({ transform: true }))
  getDatesFromBazi(@Body() body: BaziReverseDto) {
    return this.baziService.getDatesFromBazi(body.sizhu);
  }
}
